package se.webbzon.boltzmann.game.object.action;

import java.nio.ByteBuffer;

import se.webbzon.boltzmann.debug.Debug;
import se.webbzon.boltzmann.environment.AbstractEnvironment;
import se.webbzon.boltzmann.game.sequence.builder.SequenceBuilder;
import se.webbzon.boltzmann.game.sequence.builder.SequenceBuilderDeflation;
import se.webbzon.boltzmann.game.sequence.builder.SequenceBuilderException;
import se.webbzon.boltzmann.game.sequence.builder.SequenceRowInflatorGroup;
import se.webbzon.boltzmann.object.unknown.Unknown;
import se.webbzon.oschi01.friendly.Friendly;
import se.webbzon.oschi01.worldobject.WorldObject;
import se.webbzon.oschi01.worldobject.WorldStaticObject;

public class SequenceActionDeflation extends AbstractActionDeflation<WorldStaticObject> {
	
	private byte[] data;
	
	/*============================================================
	Constructors
	============================================================*/

	/** Creates a new empty sequence action deflation. **/
	public SequenceActionDeflation(	int deflationProperties,
									WorldObject[] dependencies) {
		super(deflationProperties, dependencies);
	}
	
	/** Creates a new sequence action deflation from an existing
	 * sequence action. **/
	public SequenceActionDeflation(	int deflationProperties,
									SequenceAction sequenceAction,
									SequenceBuilderDeflation builderDeflation) {
		super(	deflationProperties, 
				builderDeflation.getDependencies(),
				sequenceAction);
		data = builderDeflation.getData();
	}
	
	/*============================================================
	Public Methods
	============================================================*/

	@Override public void inflate(ByteBuffer buff) {
		super.inflate(buff);
		
		// Read from buffer
		data = new byte[buff.getInt()];
		buff.get(data, 0, data.length);
	}

	@Override public ByteBuffer deflate(int allocate) {
		ByteBuffer buff = super.deflate(allocate + 4 + data.length);
		
		// Write to buffer
		buff.putInt(data.length);
		buff.put(data);
		
		return buff;
	}
	
	/*============================================================
	Friendly Methods
	============================================================*/
	
	@Friendly WorldObject makeSequenceAction(	AbstractEnvironment env,
												SequenceRowInflatorGroup inflatorGroup) {
		// Extract dependencies for sequence builder
		int dependencies = dependencies();
		WorldStaticObject[] dependencyArray = 
				new WorldStaticObject[dependencies];
		
		for (int i = 0; i < dependencies; i++)
			dependencyArray[i] = (WorldStaticObject) getDependency(i);
		
		// Inflate sequence builder
		SequenceBuilderDeflation deflation = 
				new SequenceBuilderDeflation(data,dependencyArray);
		SequenceBuilder builder = null;
		try {
			builder = deflation.inflate(env,inflatorGroup);
		} catch (SequenceBuilderException e) {
			// Return new unknown if an error occurred
			Debug.report(e);
			return new Unknown();
		}
		
		return new SequenceAction(	env,getParent(),getColor(),
									getFrontOnly(),getRange(),builder);
	}
	
}
